package com.king.modules.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.king.common.exception.AssertHelper;
import com.king.common.utils.DictUtils;
import com.king.framework.base.service.impl.YyBaseServiceImpl;
import com.king.modules.system.domain.SysDictDataEntity;
import com.king.modules.system.domain.SysDictTypeEntity;
import com.king.modules.system.mapper.SysDictDataMapper;
import com.king.modules.system.service.SysDictDataService;
import com.king.modules.system.service.SysDictTypeService;

/**
 * 字典 业务层处理
 * 
 * @author king
 */
@Service
public class SysDictDataServiceImpl extends YyBaseServiceImpl<SysDictDataMapper, SysDictDataEntity, Long> implements SysDictDataService {
	
	@Autowired
	private SysDictDataMapper dictDataMapper;
	@Autowired
	private SysDictTypeService dictTypeService;

	
	
	
	@Override
	public void afterAdd(SysDictDataEntity entity) throws Exception {
		SysDictTypeEntity dictType = dictTypeService.findByPK(entity.getMainid());
		AssertHelper.getInstance().checkNullException(dictType, "字典类型不存在:"+entity.getMainid());
		List<SysDictDataEntity> dictDatas = dictDataMapper.selectDictDataByMainid(dictType.getId());
		DictUtils.setDictCache(dictType.getDictType(), dictDatas);
		super.afterAdd(entity);
	}


	@Override
	public void afterUpdate(SysDictDataEntity oldEntity, SysDictDataEntity entity) throws Exception {
		SysDictTypeEntity dictType = dictTypeService.findByPK(entity.getMainid());
		AssertHelper.getInstance().checkNullException(dictType, "字典类型不存在:"+entity.getMainid());
		List<SysDictDataEntity> dictDatas = dictDataMapper.selectDictDataByMainid(dictType.getId());
		DictUtils.setDictCache(dictType.getDictType(), dictDatas);
		super.afterUpdate(oldEntity, entity);
	}


	/**
	 * 根据条件分页查询字典数据
	 * 
	 * @param dictData
	 *            字典数据信息
	 * @return 字典数据集合信息
	 */
	@Override
	public List<SysDictDataEntity> selectDictDataList(SysDictDataEntity dictData) {
		return dictDataMapper.selectDictDataList(dictData);
	}


	/**
	 * 根据字典数据ID查询信息
	 * 
	 * @param dictCode
	 *            字典数据ID
	 * @return 字典数据
	 */
	@Override
	public SysDictDataEntity selectDictDataById(Long id) {
		return dictDataMapper.selectDictDataById(id);
	}


	@Override
	public int countDictDataByMainid(Long mainid) {
		return dictDataMapper.countDictDataByMainid(mainid);
	}


	@Override
	public List<SysDictDataEntity> selectDictDataByMainid(Long mainid) {
		return dictDataMapper.selectDictDataByMainid(mainid);
	}


}
